/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package blms.domain;

import static java.util.Collections.unmodifiableSet;

import java.util.HashSet;
import java.util.Set;

import blms.domain.Expression.Context;
import blms.exception.UnknownVariableException;
import blms.util.BLMSMessages;

/**
 * A {@link HandicapContext} object maps a variable name to Integer object holding
 * value. It provides two variables to an {@link Expression} to calculate player
 * handicap in current season. The available variables are "wins" and
 * "loss".
 * 
 * @see Context
 * 
 * @author João Arthur
 * @author Jonhnny Weslley
 * @author Thiago Emmanuel
 * @version 1.00, 25/04/2008
 * @since 1.0
 */
public class HandicapContext implements Context {

	private final Match match;
	private final User user;
	
	private static final String HANDICAP_WINS = "wins";
	private static final String HANDICAP_LOSSES = "loss";
	
	/**
	 * The set of the available variables from a {@link HandicapContext} instance.
	 */
	public static final Set<String> HANDICAP_AVAILABLE_VARIABLES;

	static {
		final Set<String> handicapVars = new HashSet<String>();
		handicapVars.add(HANDICAP_WINS);
		handicapVars.add(HANDICAP_LOSSES);
		HANDICAP_AVAILABLE_VARIABLES = unmodifiableSet(handicapVars);
	}

	/**
	 * Constructs a new <code>LeagueContext</code>.
	 * @param match 
	 * 			The match in the handicap context
	 * @param user
	 * 			The user in the handicap context
	 */
	public HandicapContext(Match match, User user) {
		this.match = match;
		this.user = user;
	}

	/**
	 * Returns a {@link Set} of the variable names contained in this context.
	 * The available variables are "wins" and "loss".
	 */
	public Set<String> getAvailableVariables() {
		return HANDICAP_AVAILABLE_VARIABLES;
	}
	
	/**
	 * Returns the value to which the specified variable name is associated or
	 * throws an exception if the variable does not exist.
	 * 
	 * @throws RuntimeException
	 *             if the specified variable does not exist.
	 */
	public int getVariable(final String name) {
		
		if (HANDICAP_WINS.equals(name)) {
			return booleanNumericValue(match.isTheWinner(user));

		} else if (HANDICAP_LOSSES.equals(name)) {
			return booleanNumericValue(match.isTheLoser(user));
		}
		throw new UnknownVariableException(BLMSMessages.UNKNOWN_HANDICAP_VARIABLE);
	}
	
	/**
	 * For this context, true stands for 1 and false stands for 0
	 * 
	 * @param bool
	 * @return
	 */
	private int booleanNumericValue(boolean bool) {
		return (bool == true) ? 1 : 0;
	}

}